package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.Upkeep;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface UpkeepDao {

    /**
     * 根据设备资产查询 保养计划
     *
     * @param assetNumber
     * @return
     */
    @Results(id = "upkeepResultMap", value = {
            @Result(column = "upkeep_id", property = "upkeepId", jdbcType = JdbcType.INTEGER),
            @Result(column = "upkeep_type", property = "upkeepType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_id", property = "pcbaDeviceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "asset_number", property = "assetNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "first_upkeep_time", property = "firstUpkeepTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "owner_id", property = "ownerId", jdbcType = JdbcType.INTEGER),
            @Result(column = "contact_method", property = "contactMethod", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "audit_id", property = "auditId", jdbcType = JdbcType.INTEGER),
            @Result(column = "owner_name", property = "ownerName", jdbcType = JdbcType.VARCHAR),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.UPKEEP + " where 1 = 1 " +
            "<if test='assetNumber != null and assetNumber != \"\" '>" +
            " and asset_number = #{assetNumber}" +
            "</if>" +
            "<if test='upkeepType != null and upkeepType != \"\" '>" +
            " and upkeep_type = #{upkeepType}" +
            "</if>" +
            " order by upkeep_type" +
            "</script>")
    List<Upkeep> getUpkeepByDeviceId(@Param("assetNumber") String assetNumber, @Param("upkeepType") Integer upkeepType);

    /**
     * 查询所有的保养计划
     *
     * @return
     */
    @ResultMap(value = "upkeepResultMap")
    @Select("select * from " + DB1TableName.UPKEEP)
    List<Upkeep> getAllUpkeep();

    /**
     * 判断该资产编号的设备是否存在保养计划
     *
     * @param assetNumber
     * @param upkeepId
     * @return
     */
    @Select("select * from " + DB1TableName.UPKEEP + " where asset_number = #{assetNumber} and upkeep_id != #{upkeepId}")
    @ResultMap(value = "upkeepResultMap")
    List<Upkeep> judgeExistUpkeepByAssetNumber(@Param("assetNumber") String assetNumber, @Param("upkeepId") Integer upkeepId);

    /**
     * 根据id查询
     *
     * @param upkeepId
     * @return
     */
    @ResultMap(value = "upkeepResultMap")
    @Select("select * from " + DB1TableName.UPKEEP + " where upkeep_id = #{upkeepId}")
    Upkeep getById(@Param("upkeepId") Integer upkeepId);

    /**
     * 新增
     *
     * @param upkeep
     * @return
     */
    @Insert("insert into " + DB1TableName.UPKEEP + "(upkeep_type,asset_number,pcba_device_id,create_time,first_upkeep_time,owner_id,contact_method,version,audit_id,owner_name) values " +
            "(#{upkeep.upkeepType},#{upkeep.assetNumber},#{upkeep.pcbaDeviceId},#{upkeep.createTime},#{upkeep.firstUpkeepTime},#{upkeep.ownerId},#{upkeep.contactMethod},#{upkeep.version},#{upkeep.auditId},#{upkeep.ownerName})")
    Integer addUpkeep(@Param("upkeep") Upkeep upkeep);

    /**
     * 更新
     *
     * @param upkeep
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.UPKEEP +
            "<set>" +
            "<if test='upkeep.firstUpkeepTime != null '>" +
            " first_upkeep_time = #{upkeep.firstUpkeepTime}," +
            "</if>" +
            "<if test='upkeep.ownerId != null and upkeep.ownerId != \"\" '>" +
            " owner_id = #{upkeep.ownerId}," +
            "</if>" +
            "<if test='upkeep.contactMethod != null and upkeep.contactMethod != \"\" '>" +
            " contact_method = #{upkeep.contactMethod}," +
            "</if>" +
            "<if test='upkeep.version != null and upkeep.version != \"\" '>" +
            " version = #{upkeep.version}," +
            "</if>" +
            "<if test='upkeep.auditId != null and upkeep.auditId != \"\" '>" +
            " audit_id = #{upkeep.auditId}," +
            "</if>" +
            "<if test='upkeep.ownerName != null and upkeep.ownerName != \"\" '>" +
            " owner_name = #{upkeep.ownerName}," +
            "</if>" +
            "<if test='upkeep.updateTime != null '>" +
            " update_time = #{upkeep.updateTime}," +
            "</if>" +
            "</set>" +
            " where upkeep_id = #{upkeep.upkeepId}" +
            "</script>")
    Integer updateUpkeep(@Param("upkeep") Upkeep upkeep);

    /**
     * 删除
     *
     * @param upkeepId
     * @return
     */
    @Delete("delete from " + DB1TableName.UPKEEP + " where upkeep_id = #{upkeepId}")
    Integer deleteUpkeepById(@Param("upkeepId") Integer upkeepId);

    /**
     * 根据资产编号和upkeepType删除
     *
     * @param assetNumber
     * @param upkeepType
     * @return
     */
    @Delete("delete from " + DB1TableName.UPKEEP + " where asset_number = #{assetNumber} and upkeep_type = #{upkeepType}")
    Integer deleteUpkeepByAssetNumberAndUpkeepType(@Param("assetNumber") String assetNumber, @Param("upkeepType") Integer upkeepType);
}
